<?php
namespace Api\Model;
use Common\Model\FileModel as BashModel;
use Api\Model\LogInfoModel;
use Common\Model\FileLinkModel;
use Api\Model\TagLinkModel;
use Think\Image;
/**
 * 文件相关表操作
 * 
 * @author xiaoyutab<xiaoyutab@qq.com>
 * @version v1.0.0
 * @copyright (c) 2017, xiaoyutab
 * @adtime 2017-07-13 20:32:43
 */
class FileModel extends BashModel {
    protected $link_tag = null;
    protected $log = null;
    protected $link = null;


    /**
     * 获取文件链接表
     * 
     * @return type
     */
    public function get_link_tag(){
        if($this->link_tag){
            return $this->link_tag;
        }
        $this->link_tag = new TagLinkModel();
        return $this->link_tag;
    }
    
    /**
     * 获取日志详情表
     * @return type
     */
    public function get_loginfo(){
        if($this->log){
            return $this->log;
        }
        $this->log = new LogInfoModel();
        return $this->log;
    }

    /**
     * 获取表编号
     * 
     * @param type $tab
     * @return type
     */
    public function get_table_id($tab = ''){
        return $this->get_loginfo()->get_id($tab);
    }
    
    /**
     * 获取日志详情表
     * @return type
     */
    public function get_link(){
        if($this->link){
            return $this->link;
        }
        $this->link = new FileLinkModel();
        return $this->link;
    }
    
    /**
     * 获取图片信息
     * 
     * @param type $table
     * @param type $id
     * @param type $name
     * @param type $field
     * @param type $list
     * @return boolean
     */
    public function get_img_type($table = 'tag',$id = 0,$name = false,$field = false,$list_t = false){
        if($id <= 0){
            return FALSE;
        } else if(empty ($table)){
            return FALSE;
        }
        $where = array(
            'target_name' => $this->get_table_id($table),
            'target_id' => $id
        );
        if($name !== FALSE && empty($name)){
            $where['file_name'] = array('exp','IS NULL');
        } else if($name !== FALSE){
            $where['file_name'] = trim($name);
        }
        $list = $this->get_link()->where($where)->getList(0,999);
        foreach($list as $k => $v){
            $list[$k]['file_info'] = $this->get_info($v['file_id']);
        }
        if($field){
            if($list_t){
                if(empty($list)){
                    return '';
                }
                return !empty($list[0]['file_info'][$field])?$list[0]['file_info'][$field]:$list[0]['file_info']['file_path'];
            }
            $re = array();
            foreach($list as $v){
                $re[] = !empty($v['file_info'][$field])?$v['file_info'][$field]:$v['file_info']['file_path'];
            }
            return $re;
        }
        return $list;
    }
}